using Godot;

public class SteamBoilerTankBlock : BlockMaterial
{
    public SteamBoilerTankBlock() : base("steam_boiler_tank")
    {
        DisPlayName = "蒸汽锅炉";
        StateMax = 2;
        View = true;
        Collide = true;
        CreateBlockEntity = () => new SteamBoilerTankEntity();
        TickEvent = Tick;
        imageset = new ImageSet { size = new Vector2(32, 32) };
    }
    public override bool OnUse(PlayerUseBlockEvent e)
    {
        SteamBoilerTankView SBTV = GD.Load<PackedScene>("res://Tscn/Store/SteamBoilerTank.tscn").Instance<SteamBoilerTankView>();
        SBTV.SetBlock(e.block);
        e.player.view.SetOpenView(SBTV);
        return true;
    }
    public void Tick(UpdataTickEvent e)
    {
        const int speed = 20;
        SteamBoilerTankEntity sbte = (SteamBoilerTankEntity)e.block.blockEntity;
        //向周围输出蒸汽
        if (sbte.steam.amount > 0)
        {
            float value = speed;
            if (sbte.steam.amount < speed) value = sbte.steam.amount;
            if (e.TopBlock().GetFluid("steam") != null && e.TopBlock().GetFluid("steam").AddAmount(value))
            {
                sbte.steam.amount -= value;
            }
            else
            if (e.BottomBlock().GetFluid("steam") != null && e.BottomBlock().GetFluid("steam").AddAmount(value))
            {
                sbte.steam.amount -= value;
            }
            else
            if (e.LeftBlock().GetFluid("steam") != null && e.LeftBlock().GetFluid("steam").AddAmount(value))
            {
                sbte.steam.amount -= value;
            }
            else
            if (e.RightBlock().GetFluid("steam") != null && e.RightBlock().GetFluid("steam").AddAmount(value))
            {
                sbte.steam.amount -= value;
            }
        }


        //
        if (sbte.water.amount <= sbte.water.max - 1000)
        {
            ItemStack waterin = sbte.store.GetItemStack(4);
            if (
                 waterin != null &&
                (
                    waterin.Name == "bucket_water"
                )

            )
            {
                ItemStack bucket = new ItemStack(Materials.Bucket);
                if (sbte.store.MergetItem(5, bucket))
                {
                    sbte.store.SubAmount(4);
                    sbte.water.AddAmount(1000);
                }
            }
        }
        if (sbte.water.amount >= 1000)
        {
            ItemStack waterin = sbte.store.GetItemStack(4);
            if (
                 waterin != null &&
                (
                    waterin.Name == "bucket"
                 )
            )
            {
                ItemStack bucket_water = new ItemStack(Materials.Bucket_Water);
                if (sbte.store.MergetItem(5, bucket_water))
                {
                    sbte.store.SubAmount(4);
                    sbte.water.amount -= 1000;
                }
            }
        }
        if (sbte.fuel.amount <= 0)
        {
            bool has = false;
            for (int i = 0; i < 4; i++)
            {
                ItemStack item = sbte.store.GetItemStack(i);
                if (item != null && item.getMaterial().Fuel != 0)
                {
                    sbte.fuel.max = item.getMaterial().Fuel;
                    sbte.fuel.amount = sbte.fuel.max;
                    sbte.store.SubAmount(i);
                    has = true;
                    break;
                }
            }
            if (!has && sbte.thermal.amount >= 0.2f) sbte.thermal.amount -= 0.2f;
        }
        else
        {

            //sbte.thermal.amount += 0.1f;
            if (sbte.thermal.AddAmount(0.1f))
            {
                sbte.fuel.amount -= 1;
            }
        }
        if (sbte.thermal.amount >= 100)
        {
            if (sbte.water.amount > 0)
            {

                if (sbte.steam.AddAmount(10))
                {
                    sbte.water.amount -= 1;
                }
            }
        }
    }
}